import { defineStore } from 'pinia'
import request from '@/utils/request'

import { ApiRes, getCateList } from '@/types/layoutTs/layoutType'
import { topCategory } from '@/store/constants'

// [居家,这些导航连接的假数据,应对网络慢]
const handleCate = topCategory.map((item) => {
  return { name: item }
})

export default defineStore('category', {
  state: () => ({
    // 导航连接的数据
    cateList: handleCate as getCateList[] // 先用假数据,请求回来之后用 真实数据
  }),
  actions: {
    // 获取导航链接数据的方法
    async getCateList() {
      const res = await request.get<ApiRes>('/home/category/head')
      // 在赋值之前给每一个item加show属性值是false
      res.data.result.forEach((item) => (item.show = false))
      this.cateList = res.data.result
      console.log('仓库1', res.data.result)
    },
    // 鼠标进入设置对应项的show值为true，
    layshow(id: string) {
      const obj = this.cateList.find((item) => item.id === id)
      obj!.show = true
      // console.log(`obj入`, obj)
    },
    // 鼠标离开设置对应项的show值为false
    layhide(id: string) {
      const obj = this.cateList.find((item) => item.id === id)
      obj!.show = false
      // console.log(`obj出`, obj)
    }
  },

  getters: {}
})
